import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'

import { createRouter, createWebHistory } from 'vue-router'
import type { AppRouteRecordRaw } from './types'
// 白名单
export const WHITE_NAME_LIST: string[] = ['/login']

// 基础路由
export const BASE_ROUTER: Array<AppRouteRecordRaw> = [
  {
    path: '/',
    name: 'Main',
    hidden: true,
    component: () => import('@/views/Home/index.vue'),
  },
  {
    path: '/login',
    name: 'Login',
    hidden: true,
    component: () => import('@/views/Login/index.vue'),
  },
]

// 错误路由
export const ERROR_ROUTER_MAP: Array<AppRouteRecordRaw> = [
  {
    path: '/403',
    hidden: true,
    component: () => import('@/views/Exception/403.vue'),
  },
  {
    path: '/404',
    hidden: true,
    component: () => import('@/views/Exception/404.vue'),
  },
  {
    path: '/500',
    hidden: true,
    component: () => import('@/views/Exception/500.vue'),
  },
  {
    path: '/:catchAll(.*)',
    redirect: '/404',
    hidden: true,
  },
]

export const basicRoutes = [...BASE_ROUTER, ...ERROR_ROUTER_MAP]

export const router = createRouter({
  // 创建一个 hash 历史记录。
  history: createWebHistory(import.meta.env.VITE_APP_PUBLIC_PATH),
  // 应该添加到路由的初始路由列表。
  routes: basicRoutes as unknown as RouteRecordRaw[],
  // 是否应该禁止尾部斜杠。默认为假
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

// 配置路由器
export function setupRouter(app: App<Element>) {
  app.use(router)
}
